package com.ruoyi.web.controller.ksdH5;



import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.Constans;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.vo.cpc.CpcUtils;

/**
 * ksdH5 计划
 * 
 * @author ruoyi
 * @date 2019-06-27
 */
@Controller		 
@RequestMapping("/h5/plan")
public class KsdH5PlanController 
{
	
	 /**
	  * 跳转计划列表页面
	  * @param request
	  * @param response
	  * @return
	  */
	 @GetMapping("list")
	 public String toListPage(HttpServletRequest request,HttpServletResponse response) {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
					return "h5/realName/realName";
				}
		return "h5/plan/planList";
	 } 
	 

	 /**
	  * 跳转计划详情页面
	  * @param request
	  * @param response
	  * @return
	  */
	 @GetMapping("detail")
	 public String toItemPage(HttpServletRequest request,HttpServletResponse response,ModelMap map) {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
					return "h5/realName/realName";
				}
			HttpSession session = request.getSession();
			 String planId = request.getParameter("id");
			Object token = session.getAttribute("token");
			 String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="planId="+planId;
			String  result = HttpUtils.sendGet(Constans.KSD_APP_H5_URL+"/p/getPlanDetails", params,head);
		   map.addAttribute("result", Ksdh5Util.getMap(result));
		   return "h5/plan/item";
	 } 
	 
	 /**
	  * 取消计划
	  * @param request
	  * @param response
	  * @return
	  */
	 @PostMapping("cancel")
	 @ResponseBody
	 public Map<String, Object> cancel(HttpServletRequest request,HttpServletResponse response) {
		 Map<String, Object> map = new HashMap<>();
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				map.put("status", "out");
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
				 map.put("status", "NoReal");
				}
			
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			 String planId = request.getParameter("planId");
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="planId="+planId;
			String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/p/changePlanStatus", params,head);
			return Ksdh5Util.getMap(result);
	 } 
	 
	 /**
	  * 查询计划
	  * @param request
	  * @param response
	  * @return
	  */
	 @PostMapping("list")
	 @ResponseBody
	 public Map<String, Object> planlist(HttpServletRequest request,HttpServletResponse response) {
		 Map<String, Object> map = new HashMap<>();
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				map.put("status", "out");
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
				 map.put("status", "NoReal");
				}
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="";
			String  result = HttpUtils.sendGet(Constans.KSD_APP_H5_URL+"/main/palnData", params,head);
			return Ksdh5Util.getMap(result);
	 } 
	 
	 /**
	  * 跳转添加计划页面
	  * @param request
	  * @param response
	  * @return
	  */
	 @GetMapping("add")
	 public String add(HttpServletRequest request,HttpServletResponse response) {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
				 return "h5/realName/realName";
				}
		return "h5/plan/add";
	 } 
	 /**
	  * 创建计划
	  * @param request
	  * @param response
	  * @return
	  */
	 @PostMapping("/add")
	 @ResponseBody
	 public Map<String, Object> addPlan(HttpServletRequest request,HttpServletResponse response)
	 {	
		Map<String, Object> map = new HashMap<>();
		//判断session
		boolean checkSession = Ksdh5Util.checkSession(request, response);
		if (!checkSession) {
			map.put("status", "out");
			return map;
		}
		//判断实名认证
		 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
		 if (!checkRealName) {
				map.put("status", "noReal");
				return map;
			}
		HttpSession session = request.getSession();
		Object token = session.getAttribute("token");
		String cityName = request.getParameter("cityName");
		if (cityName.indexOf("-")!=-1) {
			String[] split = cityName.split("-");
			cityName = split[1];
		}
		String planMoney = request.getParameter("planMoney");
		String dateStr = request.getParameter("dateStr");
		String cardNo = request.getParameter("cardNo");
		String second = request.getParameter("second");
		String bindID = request.getParameter("bindID");
		String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
		String params="cityCode="+CpcUtils.findCodeByName(cityName)+"&"
				     +"planMoney="+planMoney+"&" 
				     +"dateStr="+dateStr+"&" 
				     +"cardNo="+cardNo+"&" 
				     +"second="+second+"&" 
				     +"bindID="+bindID;
		params = params.replace(" ", "");//去掉所有空格
		String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/p/createPlan", params,head);
		return Ksdh5Util.getMap(result);
	 }  
	 
	 /**
	  * 提交计划
	  * @param request
	  * @param response
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 @PostMapping("/submitPlan")
	 @ResponseBody
	 public Map<String, Object> submitPlan(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException
	 {	
		Map<String, Object> map = new HashMap<>();
		//判断session
		boolean checkSession = Ksdh5Util.checkSession(request, response);
		if (!checkSession) {
			map.put("status", "out");
			return map;
		}
		//判断实名认证
		 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
		 if (!checkRealName) {
				map.put("status", "noReal");
				return map;
			}
		HttpSession session = request.getSession();
		Object token = session.getAttribute("token");
		String cityName = request.getParameter("cityName");
		if (cityName.indexOf("-")!=-1) {
			String[] split = cityName.split("-");
			cityName = split[1];
		}
		String acqCode = request.getParameter("acqCode");
		String planAmt = request.getParameter("planAmt");
		String cbAmt = request.getParameter("cbAmt");
		String planList = request.getParameter("planList");
		String rate = request.getParameter("rate");
		String endTime = request.getParameter("endTime");
		String tranPassword = request.getParameter("tranPassword");
		tranPassword = Ksdh5Util.md5Str(tranPassword);//加密
		String rate_money = request.getParameter("rate_money");
		String times_money = request.getParameter("times_money");
		String toalFree = request.getParameter("toalFree");
		String free_free = request.getParameter("free_free");
		String everyNum = request.getParameter("everyNum");
		
		String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
		String params="cityNo="+CpcUtils.findCodeByName(cityName)+"&"
				     +"acqCode="+URLEncoder.encode(acqCode,"utf-8")+"&" 
				     +"planAmt="+URLEncoder.encode(planAmt,"utf-8")+"&" 
				     +"cbAmt="+URLEncoder.encode(cbAmt,"utf-8")+"&"
				     +"planList="+URLEncoder.encode(planList,"utf-8")+"&"
				     +"rate="+URLEncoder.encode(rate,"utf-8")+"&"
				     +"endTime="+URLEncoder.encode(endTime,"utf-8")+"&"
				     +"tranPassword="+URLEncoder.encode(tranPassword,"utf-8")+"&"
				     +"rate_money="+URLEncoder.encode(rate_money,"utf-8")+"&"
				     +"times_money="+URLEncoder.encode(times_money,"utf-8")+"&"
				     +"toalFree="+URLEncoder.encode(toalFree,"utf-8")+"&"
				     +"free_free="+URLEncoder.encode(free_free,"utf-8")+"&"
				     +"everyNum="+URLEncoder.encode(everyNum,"utf-8");
		params = params.replace(" ", "");//去掉所有空格
		String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/p/submitPlan", params,head);
		return Ksdh5Util.getMap(result);
	 }  
	 
	 
	 /**
	  * 发送开通通道验证码
	  * @param request
	  * @param response
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 @PostMapping("/sendOpenAcqCode")
	 @ResponseBody
	 public Map<String, Object> sendOpenAcqCode(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException
	 {	
		Map<String, Object> map = new HashMap<>();
		//判断session
		boolean checkSession = Ksdh5Util.checkSession(request, response);
		if (!checkSession) {
			map.put("status", "out");
			return map;
		}
		HttpSession session = request.getSession();
		Object token = session.getAttribute("token");
		String cardid =request.getParameter("cardid");
		String acqcode =request.getParameter("acqcode");
		String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
		String params="cardid="+cardid+"&"
				      +"acqcode="+acqcode;
		params = params.replace(" ", "");//去掉所有空格
		String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/p/prebind", params,head);
		return Ksdh5Util.getMap(result);
	 }  
	 
	 /**
	  * 提交开通通道
	  * @param request
	  * @param response
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 @PostMapping("/submitAcq")
	 @ResponseBody
	 public Map<String, Object> submitAcq(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException
	 {	
		Map<String, Object> map = new HashMap<>();
		//判断session
		boolean checkSession = Ksdh5Util.checkSession(request, response);
		if (!checkSession) {
			map.put("status", "out");
			return map;
		}
		//判断实名认证
		 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
		 if (!checkRealName) {
				map.put("status", "noReal");
				return map;
			}
		HttpSession session = request.getSession();
		Object token = session.getAttribute("token");
		String smscode = request.getParameter("smscode");
		String resultParam = request.getParameter("result");
		String acqcode = request.getParameter("acqcode");
		String cardid = request.getParameter("cardid");
		String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
		String params="smscode="+smscode+"&"
				     +"result="+URLEncoder.encode(resultParam,"utf-8")+"&" 
				     +"acqcode="+URLEncoder.encode(acqcode,"utf-8")+"&" 
				     +"cardid="+URLEncoder.encode(cardid,"utf-8");
		params = params.replace(" ", "");//去掉所有空格
		String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/p/confirmbind", params,head);
		return Ksdh5Util.getMap(result);
	 }  
	 
	 
}